package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.blog.vo.ArticleTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {

    private final IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<ArticleType>> findAll() {
        List<ArticleType> list = articleTypeService.findAll();
        return R.ok(list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    public R<ArticleType> findOne(@PathVariable Long id) {
        ArticleType one = articleTypeService.findOne(id);
        return R.ok(one);
    }

    @PostMapping
    @ApiOperation(value = "分页加条件查询")
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery ArticleTypeQuery) {

        List<ArticleType> lists = articleTypeService.queryPage(ArticleTypeQuery);
        return new PageInfo<ArticleType>(lists);//limit a,b
    }

    @PutMapping
    @ApiOperation(value = "根据id更新或增加")
    public R<Void> addOrUpdateArticleType(@RequestBody ArticleType ArticleType) {
        try {
            if (ArticleType.getId() == null || ArticleType.getId() == 0) {
                articleTypeService.addArticleType(ArticleType);
            } else {
                articleTypeService.updateArticleType(ArticleType);
            }
        } catch (Exception e) {
            return R.fail("用户名已经存在");
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个")
    public R<Void> deleteArticleType(@PathVariable Long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @PatchMapping//批量请求
    @ApiOperation(value = "批量删除")
    public R<Void> batchArticleTypes(@RequestBody Long[] ids) {
        try {
            articleTypeService.batchArticleTypes(ids);
        } catch (Exception e) {
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @GetMapping("/getTypeTree")
    @ApiOperation(value = "查询类型树")
    public R<List<ArticleType>> getTypeTree(Long id) {
        List<ArticleType> articleTypes = articleTypeService.getTypeTree(id);//假设法
        return R.ok(articleTypes);//alt enter生成返回值
    }

    //get:/articleType/articleTypeStatistics
    @GetMapping("/articleTypeStatistics")
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        List<ArticleTypeVo> list = articleTypeService.getArticleTypeStatistics();//假设法
        return list;//alt enter
    }
}
